package com.lf.web.service.system.module;

import com.lf.web.service.ResponseCode;
import com.lf.web.service.ResponseInfo;
import com.lf.web.service.ServiceSupport;
import org.apache.log4j.Logger;

import java.util.Date;
import java.util.HashMap;

public class ModuleSaveService extends ServiceSupport {

    private static final Logger LOGGER = Logger.getLogger(ModuleSaveService.class);
    private ModuleDAO moduleDAO;

    public void setModuleDAO(ModuleDAO moduleDAO) {
        this.moduleDAO = moduleDAO;
    }

    public ModuleSaveService() {
    }

    public String execute() {
        Module module = new Module();
        boolean result = form.getCluster(module);
        if (result) {
            String id;
            if (module.containsKey(Module.ID)) {
                id = module.getId();
            } else {
                id = module.setId();
                module.put(Module.CREATED, new Date());
            }
            module.put(module.UPDATED, new Date());
            HashMap<String, Object> channel = new HashMap<>();
            module.put(module.CHANNEL, channel);
            moduleDAO.save(module);
            ResponseInfo rj = new ResponseInfo(ResponseCode.FINISH);
            rj.put("id", id);
            out(rj);
        } else {
            LOGGER.error("错误参数：" + module);
            return error(404);
        }
        LOGGER.debug("接收参数：" + module);
        return NONE;
    }
}
